package com.zesionlee.server.config

//import com.zesionlee.base.annotation.ProjectLog
//import com.zesionlee.base.annotation.Slf4j2
//import com.zesionlee.base.annotation.Slf4j2.Companion.log
import com.zesionlee.server.result.Result
import jakarta.servlet.http.HttpServletRequest
import org.aspectj.lang.JoinPoint
import org.aspectj.lang.ProceedingJoinPoint
import org.aspectj.lang.annotation.AfterReturning
import org.aspectj.lang.annotation.Around
import org.aspectj.lang.annotation.Aspect
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Component
import org.springframework.web.context.request.RequestAttributes
import org.springframework.web.context.request.RequestContextHolder
import java.util.*


@Aspect
@Component
//@Slf4j2
class ProjectLogAspect(val request: HttpServletRequest) {

    private val log = LoggerFactory.getLogger(ProjectLogAspect::class.java)

    @AfterReturning(value = "within(com.zesionlee.*.controller.*)", returning = "rtv")
    fun afterReturningImp(joinPoint: JoinPoint?, rtv: Any) {
        val requestAttributes = RequestContextHolder.getRequestAttributes()!!
        val request = requestAttributes.resolveReference(RequestAttributes.REFERENCE_REQUEST) as HttpServletRequest?
        if (request != null) {

            val url = request.requestURI.lowercase(Locale.getDefault())

            if (url.contains("download")) {
                log.info("请求： Method:{} Url:{}", request.method, request.requestURI)
            } else {
//                val res = rtv.toString()
                if (rtv.javaClass.name.equals("com.zesionlee.server.result.Result")) {
                    log.info(
                        "请求： Method:{} Url:{} code:{} Result:{}",
                        request.method,
                        request.requestURI,
                        (rtv as Result<*>).code,
                        rtv.msg
                    )
                } else {
                    log.info("请求： Method:{} Url:{}", request.method, request.requestURI)
                }
            }
        }
    }


}